package com.lbn.monitor.widget.calendar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lbn.monitor.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import indi.free.widgets.custom_recycler_view.BaseRecyclerAdapter;

/**
 * Created by lbn on 2016/9/13.
 */
public class CalendarFragment extends Fragment implements BaseRecyclerAdapter.OnItemClickListener {


    private View root;
    private RecyclerView recyclerView;
    private CalendarAdapter adapter;
    private int pos;
    private Calendar calendar = Calendar.getInstance();

    private int cYear;
    private int cMonth;
    private int cDay;

    private int table = 7*6;

    private int year;
    private int month;
    private int week;

    private int position;

    private List<CalendarData> data;
    public CalendarFragment(){
    }


    public static CalendarFragment newInstance(int position) {
        CalendarFragment fragment = new CalendarFragment();
        Bundle arg = new Bundle();
        arg.putInt("date",position);
        fragment.setArguments(arg);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        this.position = getArguments().getInt("date");

        cYear = calendar.get(Calendar.YEAR);
        cMonth = calendar.get(Calendar.MONTH)+1;
        cDay = calendar.get(Calendar.DAY_OF_MONTH);


        year = position/12;
        month = position - year*12;
        if(position%12!=0){
            year= year+1;
        }
        if(month==0){
            month = 12;
        }
        calendar.set(year,month - 1,1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        week = calendar.get(Calendar.DAY_OF_WEEK)-1;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if(root==null){
            root = inflater.inflate(R.layout.fragment_calendar,container,false);
        }

        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(adapter==null){
            recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(),7));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            initData();
            adapter = new CalendarAdapter(getContext(),data,month);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(this);
            if(pos!=0){
                CalendarObserver.subscribe(adapter,pos);
            }
        }
    }

    /**
     * 初始化日期顺序
     */
    public void initData(){
        if(data!=null){
            data = null;
        }
        data = new ArrayList<>();
        int monthDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//        Log.e("ccc","year:"+year);
//        Log.e("ccc","month:"+month);
//        Log.e("ccc","week:"+week);
//        Log.e("ccc","monthDay:"+monthDay);
        int lastMonthDay;
        if((month - 1)==0)
            calendar.set(year-1,11,1);
        else
            calendar.set(Calendar.MONTH,month - 2);
        lastMonthDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        switch (week){
            case 0:
                for(int i = 0 ;i < table;i++){
                    CalendarData cData = new CalendarData();
                    if(i>=monthDay){
                        cData.day = i - monthDay+1;
                        cData.month = month+1>12?1:month+1;
                        if(month+1>12){
                            cData.month =1;
                            cData.year = year+1;
                        } else{
                            cData.month = month+1;
                            cData.year = year;
                        }
                    }else{
                        cData.day = i+1;
                        cData.month = month;
                        cData.year = year;
                    }
                    state(cData);
                    data.add(cData);
                }
                break;
            default:
                int lastMonth = calendar.get(Calendar.MONTH)+1;
                int l = lastMonthDay - week;
                for(int i = l;i< lastMonthDay;i++){
                    CalendarData cData = new CalendarData();
                    cData.year = lastMonth>=month?year-1:year;
                    cData.month = lastMonth;
                    cData.day = i+1;
                    state(cData);
                    data.add(cData);
                }
                for(int i=0;i<monthDay;i++){
                    CalendarData cData = new CalendarData();
                    cData.year = year;
                    cData.month = month;
                    cData.day = i+1;
                    state(cData);
                    data.add(cData);
                }

                int t =table-(monthDay+week);
                for(int i=0 ;i<t;i++){
                    CalendarData cData = new CalendarData();
                    if(month+1>12){
                        cData.year = year+1;
                        cData.month = 1;
                    } else{
                        cData.year = year;
                        cData.month = month+1;
                    }
                    cData.day = i+1;
                    state(cData);
                    data.add(cData);
                }
                break;
        }
    }

    /**
     * 判断今天日期的选中
     */
    private void state(CalendarData cData){
        if(cYear == cData.year && (cMonth == cData.month && month == cData.month) && cDay == cData.day){
            cData.state = 1;
            pos = data.size();
        }
    }

    /**
     * 点击事件
     */

    @Override
    public void onItemClick(View itemView, int pos) {
        int itemMoth = adapter.getItem(pos).month;
        int itemYear = adapter.getItem(pos).year;
        if(itemMoth!=month){
            if(itemYear > year){
                setCurrentItem(position+1,pos);
            } else if(itemYear<year){
                setCurrentItem(position-1,pos);
            } else{
                if(itemMoth>month){
                    setCurrentItem(position+1,pos);
                } else{
                    setCurrentItem(position-1,pos);
                }
            }
            return;
        }
        CalendarObserver.subscribe(adapter,pos);
    }


    public CalendarAdapter getAdapter() {
        return adapter;
    }


    private int getItemPos(CalendarAdapter adapter ,CalendarData item){
        int count = adapter.getItemCount();
        int year = item.year;
        int day = item.day;
        int month = item.month;
//        Log.e("CalendarData","year:"+year);
//        Log.e("CalendarData","month:"+month);
//        Log.e("CalendarData","day:"+day);
        for(int i =0;i<count;i++){
            CalendarData data = adapter.getItem(i);
            if(data.day == day && data.year == year && data.month == month){
                return i;
            }
        }
        return -1;
    }

    private void setCurrentItem(int position,int itemPos){
        CalendarAdapter adapter = CalendarObserver.getFragment(position).getAdapter();
        CalendarObserver.subscribe(adapter,getItemPos(adapter,this.adapter.getItem(itemPos)));
        CalendarObserver.getViewPager().setCurrentItem(position);
    }

}
